import api from '../../../api/index'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        img: '',
        bgHexagon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/bg-hexagon.png',
        blueHexagon: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/dailyAttendance/bluesix.png',
        foodPatternIcon: [
            {
                name: '钙',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_sc.png'
            },
            {
                name: '锌',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_jg.png'
            },
            {
                name: '钠',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_nn.png'
            },
            {
                name: '矿物质',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_zwy.png'
            },
            {
                name: '镁',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_sg.png'
            },
            {
                name: '碘',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_mls.png'
            },
            {
                name: '铁',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_rou.png'
            }
        ],
        filteredDataT: [],
        filteredDataM: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getlist()
        var { id } = options
        if (id) {
            this.setData({
                id
            })
        }
        this.getgoodload(id)
        // this.getgoodload('2252')
    },
    getlist() {
        api.post('/api/hxhealth/datetimefood/dict/list', {
            data: {
            }
        }).then(res => {
            console.log(res);
        }).catch(e => {
            console.log(e)
        })
    },
    getgoodload(id) {
        try {
            api.get(`/api/hxhealth/datetimefood/dict/load/${id}`).then((res) => {
                console.log(res);
                // res[0].images = JSON.parse(res[0].media).images
                // res[0].zanusers = res[0].zanusers?JSON.parse(res[0].zanusers):''
                let data = res[0].items
                let filteredDataT = data.filter(item => item.group === 'T');
                console.log(filteredDataT);
                let filteredDataM = data.filter(item => item.group === 'M');
                // console.log(JSON.stringify(filteredDataM));
                // 指定排序顺序
                let order = ['钙', '锌', '钠', '镁', '碘', '铁'];
                filteredDataM.sort((a, b) => {
                    return order.indexOf(a.name) - order.indexOf(b.name);
                });
                console.log(filteredDataM);
                let filteredDataN = data.filter(item => item.group === 'N');
                // console.log(JSON.stringify(filteredDataM));
                // 指定排序顺序
                let orderN = ['蛋白质', '碳水化合物', '脂肪'];
                filteredDataN.sort((a, b) => {
                    return orderN.indexOf(a.name) - orderN.indexOf(b.name);
                });
                console.log(filteredDataN);
                this.setData({
                    authorload: res[0],
                    filteredDataT,
                    filteredDataM,
                    filteredDataN,
                    img: res[0].image,
                    loadname: res[0].name,
                    kcal: res[0].kcal
                })
            });
        } catch (e) {
            console.error(e);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})